﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using Web.Utils;

namespace Web.Page
{
    public partial class CS_CPMC_Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblTitle.Text = "修改产品名称";
                initValue();
            }
        }

        private void initValue()
        {
            CS_CPXSet_Dao dao = new CS_CPXSet_Dao();
            List<CS_CPX> list = dao.QueryAll();
            this.ddlCPXBH.DataSource = list;
            this.ddlCPXBH.DataValueField = "BH";
            this.ddlCPXBH.DataTextField = "MC";
            this.ddlCPXBH.DataBind();
            this.ddlCPXBH.Items.Insert(0, new ListItem("请选择", "0"));

            string selectedValue = Request.QueryString["cpxbh"];
            ListItem li = this.ddlCPXBH.Items.FindByValue(selectedValue);
            if(li != null)
            {
                li.Selected = true;
            }
            this.tbBH.Text = Request.QueryString["bh"];
            this.tbMC.Text = Request.QueryString["mc"];
        }

        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {
            this.lbResult.Text = "";
            this.tbMC.Text = "";
        }

      
        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {
            if (this.ddlCPXBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择线";
                return;
            }
            if (this.tbBH.Text.Trim().Equals(""))
            {
                this.lbResult.Text = "请输入编号";
                return;
            }
            CS_CPMC entity = new CS_CPMC()
            {
                CPXBH = ddlCPXBH.SelectedValue,
                BH = tbBH.Text.Trim(),
                MC = tbMC.Text.Trim()
            };

            string resultMessage = "";
            try
            {
                CS_CPMCSet_Dao dao = new CS_CPMCSet_Dao();

                if (dao.Exist(entity.CPXBH, entity.MC))
                {
                    resultMessage = "同样名称的产品名称已存在";
                    return;
                }

                int result = dao.Update(entity);

                if (result == 1)
                {
                    resultMessage = "修改成功";
                    ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "修改失败:" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/CS_CPMC_Query.aspx");
        }

    }
}